<?php

namespace app\member\controller;

use think\Controller;
use think\Db;
use think\facade\Session;

class Login extends Controller
{
	// 登陆模版
	public function index()
	{
		// 检测是否登录
		if (session::get('member_id')) {
			$this->redirect('member/Index/index');
		}
		return view('login');
	}

	// 执行登陆
	public function dologin()
	{
		if ($this->request->isAjax()) {
			$post = input('post.');
			$logname = $post['logname'];
			$logpass = $post['logpass'];
			$code = $post['code'];

			if ($logname == '' || $logpass == '') {
				return json(['msg' => '账户名或密码不可为空!', 'error' => '1']);
			}

			$user = Db::name('user')->where(array('user_account' => $logname))->find();
			if (!$user) {
				return json(['msg' => '账户不存在!', 'error' => '1']);
			}

			if (md5($logpass) !== $user['user_password']) {
				return json(['msg' => '密码输入错误!', 'error' => '1']);
			}

			if (!captcha_check($code)) {
				return json(['msg' => '验证码输入错误!', 'error' => '1']);
			};

			session('member_id', $user['user_id']);

			return json(['error' => 0, 'msg' => '登陆成功', 'url' => Url('member/Index/index')]);

		} else {
			return json(['msg' => 'reqserr!', 'error' => '1']);
		}
	}

	//退出登录
	public function logout()
	{
		Session::clear();
		session_unset();
		$this->redirect('/member');

	}
}
